#include "Shooter_PCH.h"
#include "VertexBuffer.h"

/*--------------------------------------------------------------------------*/

VertexBuffer::VertexBuffer(unsigned int numVertices) :
	numVertices(numVertices)
{
	glGenBuffers(1, &bufferHandle);
	glBindBuffer(GL_ARRAY_BUFFER, bufferHandle);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * numVertices, NULL, GL_STATIC_DRAW);
	std::cout << "Created VBO for " << numVertices << " vertices. Handle = " << bufferHandle << std::endl;
}

/*--------------------------------------------------------------------------*/

VertexBuffer::~VertexBuffer()
{
	glDeleteBuffers(1, &bufferHandle);
	std::cout << "Deleted VBO. Handle = " << bufferHandle << std::endl;
}

/*--------------------------------------------------------------------------*/

Vertex* VertexBuffer::Map()
{
	glBindBuffer(GL_ARRAY_BUFFER, bufferHandle);
	return (Vertex*) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
}

/*--------------------------------------------------------------------------*/

void VertexBuffer::Unmap()
{
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

/*--------------------------------------------------------------------------*/

void VertexBuffer::SetRawData( void* data, unsigned int offset, unsigned int size )
{
	glBindBuffer(GL_ARRAY_BUFFER, bufferHandle);
	glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
}

/*--------------------------------------------------------------------------*/

void VertexBuffer::SetVertexData( Vertex* data, unsigned int firstId, unsigned int count )
{
	glBindBuffer(GL_ARRAY_BUFFER, bufferHandle);
	glBufferSubData(GL_ARRAY_BUFFER, firstId * sizeof(Vertex), count * sizeof(Vertex), data);
										// offset                //size
}

/*--------------------------------------------------------------------------*/
